package com.example.picturestorage.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.picturestorage.bean.Publisher;
import com.example.picturestorage.service.PublisherService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PublisherController {

    private final static Logger LOGGER = LoggerFactory.getLogger(PublisherController.class);

    @Autowired
    private PublisherService publisherService;

    @PostMapping("/publisher")
    public String addPublisher(HttpServletRequest request) {
        String name = request.getParameter("publisherName");
        Publisher publisher = new Publisher();
        publisher.setPublisherName(name);
        publisherService.addPublisher(publisher);
        LOGGER.info("id " + publisher.getId());
        return "redirect:/publishers";
    }

    @GetMapping("/publishers")
    public String allPublisher(Model model) {
        List<Publisher> allPublisher = publisherService.findAllPublisher();
        model.addAttribute("all", allPublisher);
        LOGGER.info(String.valueOf(allPublisher));
        return "publisher/all";
    }

    @GetMapping("/findAllPublisherWithBooks")
    public String findAllPublisherWithBooks(Model model) {
        Page<Publisher> objects = PageHelper.startPage(1, 2);
        List<Publisher> allPublisher = publisherService.findAllPublisherWithBooks();
        model.addAttribute("all", allPublisher);
        LOGGER.info(String.valueOf(allPublisher));
        return JSONArray.toJSONString(allPublisher);
    }


    @GetMapping("/publisher/{id}")
    @ResponseBody
    public Publisher findPublisherById(@PathVariable Integer id) {
        return publisherService.findPublisherById(id);
    }

    @PostMapping("/publisher/insertPublishers")
    public int insertPublishers(@RequestBody List<Publisher> publishers) {
        if (!CollectionUtils.isEmpty(publishers)
                && !(publishers.stream().map(Publisher::getPublisherName).filter(StringUtils::isEmpty).count() == publishers.size())) {
            return publisherService.insertPublishers(publishers);
        } else return 0;
    }

    @PostMapping("/publisher/{id}")
    public String updatePublisher(HttpServletRequest request) {
        String s = request.getParameter("id");
        int id = Integer.parseInt(s);
        String name = request.getParameter("name");
        publisherService.updatePublisher(name, id);
        return "redirect:/publishers";
    }

    @DeleteMapping("/publisher")
    public String deletePublisher(HttpServletRequest request) {
        String s = request.getParameter("id");
        int id = Integer.parseInt(s);
        publisherService.deletePublisher(id);
        return "redirect:/publishers";
    }

    public static class Main {
        public static void main(String[] args) {
            String s = "";
            s += 1;
            s += 2;
            LOGGER.info(String.valueOf(Integer.parseInt(s, 10)));
        }
    }


}
